HAL CORDIC initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 CORDIC configuration panel.

  • Configure the SW layer HAL.

  • Configure the main CORDIC features.

  • Configure the CORDIC DMA(s) if the application requires the CORDIC to be used in DMA mode.

  • Configure the NVIC if the application requires the CORDIC to be used in IT mode.

  • Generate the code.

HAL CORDIC configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

Configure the CORDIC main features

Configure the main features.

CORDIC main feature configuration

DMA configuration

Select DMA Write and DMA Read and configure the CORDIC DMA channels if the application requires to use the CORDIC in DMA mode.

CORDIC DMA configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

CORDIC NVIC configuration

Generated CORDIC initialization and de-initialization sequences

The CORDIC initialization sequence is generated in the mx_cordic_hal_init function in mx_cordic.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

Note

  • In HAL1, STM32CubeMX did not generate a calculation configuration, which had to be added manually before starting the calculation process. In HAL2, STM32CubeMX2 generates the configuration, and the user only needs to add the calculation step.

STM32CubeMX2 Generated : CORDIC initialization sequence

Topic

HAL1 Code Snippet

HAL2 Code Snippet

Handle declaration

CORDIC_HandleTypeDef hcordic;
DMA_HandleTypeDef handle_GPDMA1_Channel1;
DMA_HandleTypeDef handle_GPDMA1_Channel0;
static hal_cordic_handle_t hCORDIC;
static hal_dma_handle_t hLPDMA1_CH0;
static hal_dma_handle_t hLPDMA1_CH1;

Initialization and configuration sequence.

 void MX_CORDIC_Init(void)
 {
   hcordic.Instance = CORDIC;

   if (HAL_CORDIC_Init(&hcordic) != HAL_OK)
   {
     Error_Handler();
   }
 }

void HAL_CORDIC_MspInit(CORDIC_HandleTypeDef* hcordic)
{
  if(hcordic->Instance==CORDIC)
  {

    __HAL_RCC_CORDIC_CLK_ENABLE();














    __HAL_RCC_DMA1_CLK_ENABLE();/* MX_DMA_Init */






    hdma_cordic_write.Instance = DMA1_Channel2;
    hdma_cordic_write.Init.Request = DMA_REQUEST_CORDIC_WRITE;
    hdma_cordic_write.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_cordic_write.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_cordic_write.Init.MemInc = DMA_MINC_ENABLE;
    hdma_cordic_write.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
    hdma_cordic_write.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_cordic_write.Init.Mode = DMA_NORMAL;
    hdma_cordic_write.Init.Priority = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_cordic_write) != HAL_OK)
    {
      Error_Handler();
    }


    HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);/* MX_DMA_Init */
    HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);


    __HAL_LINKDMA(hcordic,hdmaIn,hdma_cordic_write);











    hdma_cordic_read.Instance = DMA1_Channel1;
    hdma_cordic_read.Init.Request = DMA_REQUEST_CORDIC_READ;
    hdma_cordic_read.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_cordic_read.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_cordic_read.Init.MemInc = DMA_MINC_ENABLE;
    hdma_cordic_read.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
    hdma_cordic_read.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_cordic_read.Init.Mode = DMA_NORMAL;
    hdma_cordic_read.Init.Priority = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_cordic_read) != HAL_OK)
    {
      Error_Handler();
    }


    HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);/* MX_DMA_Init */
    HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);


    __HAL_LINKDMA(hcordic,hdmaOut,hdma_cordic_read);


  }

}
hal_cordic_handle_t *mx_cordic_hal_init(void)
{
  hal_cordic_config_t cordic_config;

  if (HAL_CORDIC_Init(&hCORDIC, HAL_CORDIC) != HAL_OK)
  {
    return NULL;
  }






  /* configure RCC clock activation*/
  HAL_RCC_CORDIC_EnableClock();

  /* Basic Configuration ***************************************************/
  cordic_config.function = HAL_CORDIC_FUNCTION_COSINE;
  cordic_config.in_width = HAL_CORDIC_IN_WIDTH_32_BIT;
  cordic_config.nb_args = HAL_CORDIC_NB_ARG_1;
  cordic_config.out_width = HAL_CORDIC_OUT_WIDTH_32_BIT;
  cordic_config.nb_results = HAL_CORDIC_NB_RESULT_1;
  cordic_config.scaling_factor = HAL_CORDIC_SCALING_FACTOR_0;
  cordic_config.precision = HAL_CORDIC_PRECISION_5_CYCLE;
  if (HAL_CORDIC_SetConfig(&hCORDIC,&cordic_config) != HAL_OK)
  {
    return NULL;
  }
  /* Configure the DMA Write request */
  HAL_RCC_GPDMA1_EnableClock();

  if (HAL_DMA_Init(&hGPDMA1_CH0, HAL_GPDMA1_CH0) != HAL_OK)
  {
    return NULL;
  }

  hal_dma_direct_xfer_config_t xfer_cfg_cordic_wr_dma;
  xfer_cfg_cordic_wr_dma.request         = HAL_GPDMA1_REQUEST_CORDIC_WR;
  xfer_cfg_cordic_wr_dma.direction       = HAL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  xfer_cfg_cordic_wr_dma.src_inc         = HAL_DMA_SRC_ADDR_INCREMENTED;
  xfer_cfg_cordic_wr_dma.dest_inc        = HAL_DMA_DEST_ADDR_FIXED;
  xfer_cfg_cordic_wr_dma.src_data_width  = HAL_DMA_SRC_DATA_WIDTH_WORD;
  xfer_cfg_cordic_wr_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_WORD;
  xfer_cfg_cordic_wr_dma.priority        = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;

  if (HAL_DMA_SetConfigPeriphDirectXfer(&hGPDMA1_CH0, &xfer_cfg_cordic_wr_dma) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for GPDMA1_CH0 */
  HAL_CORTEX_NVIC_SetPriority(GPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(GPDMA1_CH0_IRQn);

  /* Link the write DMA handle to the CORDIC handle */
  if (HAL_CORDIC_SetWriteDMA(&hCORDIC, &hGPDMA1_CH0) != HAL_OK)
  {
    return NULL;
  }
  /* Configure the DMA Read request */
  HAL_RCC_GPDMA1_EnableClock();

  if (HAL_DMA_Init(&hGPDMA1_CH1, HAL_GPDMA1_CH1) != HAL_OK)
  {
    return NULL;
  }

  hal_dma_direct_xfer_config_t xfer_cfg_cordic_rd_dma;
  xfer_cfg_cordic_rd_dma.request         = HAL_GPDMA1_REQUEST_CORDIC_RD;
  xfer_cfg_cordic_rd_dma.direction       = HAL_DMA_DIRECTION_PERIPH_TO_MEMORY;
  xfer_cfg_cordic_rd_dma.src_inc         = HAL_DMA_SRC_ADDR_FIXED;
  xfer_cfg_cordic_rd_dma.dest_inc        = HAL_DMA_DEST_ADDR_INCREMENTED;
  xfer_cfg_cordic_rd_dma.src_data_width  = HAL_DMA_SRC_DATA_WIDTH_WORD;
  xfer_cfg_cordic_rd_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_WORD;
  xfer_cfg_cordic_rd_dma.priority        = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;

  if (HAL_DMA_SetConfigPeriphDirectXfer(&hGPDMA1_CH1, &xfer_cfg_cordic_rd_dma) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for GPDMA1_CH1 */
  HAL_CORTEX_NVIC_SetPriority(GPDMA1_CH1_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(GPDMA1_CH1_IRQn);

  /* Link the Read DMA handle to the CORDIC handle */
  if (HAL_CORDIC_SetReadDMA(&hCORDIC, &hGPDMA1_CH1) != HAL_OK)
  {
    return NULL;
  }

  return &hCORDIC;
}

Deinitialization sequence.

void HAL_CORDIC_MspDeInit(CORDIC_HandleTypeDef* cordicHandle)
{

  if(cordicHandle->Instance==CORDIC)
  {
    __HAL_RCC_CORDIC_CLK_DISABLE();


    HAL_DMA_DeInit(cordicHandle->hdmaOut);





    HAL_DMA_DeInit(cordicHandle->hdmaIn);

  }
}
void mx_cordic_hal_deinit(void)
{
  /* CORDIC De-initialization */
  (void)HAL_CORDIC_DeInit(&hCORDIC);
  /* Disable CORDIC Clock */
  HAL_RCC_CORDIC_Reset();
  HAL_RCC_CORDIC_DisableClock();
  /* De-initialize the DMA channel */
  HAL_DMA_DeInit(&hGPDMA1_CH0);

  /* Disable the interruption for DMA */
  HAL_CORTEX_NVIC_DisableIRQ(GPDMA1_CH0_IRQn);

  /* De-initialize the DMA channel */
  HAL_DMA_DeInit(&hGPDMA1_CH1);

  /* Disable the interruption for DMA */
  HAL_CORTEX_NVIC_DisableIRQ(GPDMA1_CH1_IRQn);

}

IRQ handler functions.

void GPDMA1_Channel0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&handle_GPDMA1_Channel0);
}

void GPDMA1_Channel1_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&handle_GPDMA1_Channel1);
}

void CORDIC_IRQHandler(void)
{
  HAL_CORDIC_IRQHandler(&hcordic);
}
void LPDMA1_CH0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}

void LPDMA1_CH1_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hLPDMA1_CH1);
}

void CORDIC_IRQHandler(void)
{
  HAL_CORDIC_IRQHandler(&hCORDIC);
}